package com.dxf.hchat.service.impl;

import com.dxf.hchat.mapper.TbFriendMapper;
import com.dxf.hchat.mapper.TbFriendReqMapper;
import com.dxf.hchat.mapper.TbUserMapper;
import com.dxf.hchat.pojo.*;
import com.dxf.hchat.pojo.vo.FriendReq;
import com.dxf.hchat.pojo.vo.User;
import com.dxf.hchat.service.FriendService;
import com.dxf.hchat.utils.IdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class FriendServiceImpl implements FriendService {
    @Autowired
    private TbFriendMapper tbFriendMapper;
    @Autowired
    private TbUserMapper tbUserMapper;
    @Autowired
    private TbFriendReqMapper tbFriendReqMapper;
    @Autowired
    private IdWorker idWorker;



    @Override
    public void sendRequest(String fromUserid, String toUserid) {
        TbUser friend = tbUserMapper.selectByPrimaryKey(toUserid);
        ArrayList<TbUser> list = new ArrayList<>();
        list.add(friend);
        checkIsAllowed(fromUserid,list);//验证查询是否可以申请好友

        //添加
        TbFriendReq tbFriendReq = new TbFriendReq();
        tbFriendReq.setCreatetime(new Date());
        tbFriendReq.setFromUserid(fromUserid);
        tbFriendReq.setId(idWorker.nextId());
        tbFriendReq.setToUserid(toUserid);
        tbFriendReq.setStatus(0);
        tbFriendReq.setMessage("申请添加好友");

        tbFriendReqMapper.insert(tbFriendReq);

    }

    /**
     * 获取好友请求列表
     * @param userid
     * @return
     */
    @Override
    public List<FriendReq> findFriendReqByUserid(String userid) {

        TbFriendReqExample example = new TbFriendReqExample();
        TbFriendReqExample.Criteria criteria = example.createCriteria();
        criteria.andToUseridEqualTo(userid);
        criteria.andStatusEqualTo(0);//未处理的
        List<TbFriendReq> friendReqList = tbFriendReqMapper.selectByExample(example);
        ArrayList<FriendReq> friendReqs = new ArrayList<>();
        for (TbFriendReq tbFriendReq : friendReqList) {
            TbUser tbUser = tbUserMapper.selectByPrimaryKey(tbFriendReq.getFromUserid());
            User user =new User();
            FriendReq friendReq = new FriendReq();
            BeanUtils.copyProperties(tbUser,user);
            BeanUtils.copyProperties(user,friendReq);
            BeanUtils.copyProperties(tbFriendReq,friendReq);
            friendReqs.add(friendReq);
        }
        return friendReqs;



    }


    private void checkIsAllowed(String userId, List<TbUser> friendList){
        //1 不能自己添加自己为好友
        for (TbUser friend : friendList) {
            if (friend.getId().equals(userId)){
                throw new RuntimeException("自己添加自己为好友");
            }
        }


        //2 已经成为好友的不能添加
        TbFriendExample friendExample = new TbFriendExample();
        TbFriendExample.Criteria criteria = friendExample.createCriteria();
        criteria.andUseridEqualTo(userId);

        for (TbUser friend : friendList) {
            criteria.andFriendsIdEqualTo(friend.getId());
            List<TbFriend> friendList1 = tbFriendMapper.selectByExample(friendExample);
            if (friendList1!=null && friendList1.size()>0){
                throw new RuntimeException("已经添加为好友");
            }
        }

        //3 已经发出过请求的,不能重复发出请求
        TbFriendReqExample reqExample = new TbFriendReqExample();
        TbFriendReqExample.Criteria reqCriteria = reqExample.createCriteria();
        reqCriteria.andFromUseridEqualTo(userId);
        reqCriteria.andStatusEqualTo(0);//0表示未处理
        for (TbUser friend : friendList) {
            reqCriteria.andToUseridEqualTo(friend.getId());

            List<TbFriendReq> reqFriendList = tbFriendReqMapper.selectByExample(reqExample);
            if (reqFriendList!=null && reqFriendList.size()>0){
                throw new RuntimeException("不能重复添加为好友");
            }
        }

    }

    /**
     * 接受好友申请
     * @param reqid
     */

    @Override
    public void acceptFriendReq(String reqid) {
        //status 改为1
        TbFriendReq tbFriendReq = tbFriendReqMapper.selectByPrimaryKey(reqid);
        tbFriendReq.setStatus(1);
        tbFriendReqMapper.updateByPrimaryKey(tbFriendReq);


        //朋友表加入2条记录
        TbFriend tbFriend1 = new TbFriend();

        tbFriend1.setId(idWorker.nextId());
        tbFriend1.setCreatetime(new Date());
        tbFriend1.setUserid(tbFriendReq.getFromUserid());
        tbFriend1.setFriendsId(tbFriendReq.getToUserid());
        tbFriendMapper.insert(tbFriend1);

        TbFriend tbFriend2 = new TbFriend();
        tbFriend2.setId(idWorker.nextId());
        tbFriend2.setCreatetime(new Date());
        tbFriend2.setUserid(tbFriendReq.getToUserid());
        tbFriend2.setFriendsId(tbFriendReq.getFromUserid());
        tbFriendMapper.insert(tbFriend2);


    }

    /**
     * 拒绝好友申请
     * @param reqid
     */
    @Override
    public void ignoreFriendReq(String reqid) {
        //status 改为1
        TbFriendReq tbFriendReq = tbFriendReqMapper.selectByPrimaryKey(reqid);
        tbFriendReq.setStatus(1);
        tbFriendReqMapper.updateByPrimaryKey(tbFriendReq);
    }

    /**
     * 好友通讯录
     * @param userid
     * @return
     */
   public List<User> findFriendByUserid(String userid){

       TbFriendExample example = new TbFriendExample();
       TbFriendExample.Criteria criteria = example.createCriteria();
       criteria.andUseridEqualTo(userid);
       List<TbFriend> tbFriends = tbFriendMapper.selectByExample(example);

       ArrayList<User> users = new ArrayList<>();

       for (TbFriend tbFriend : tbFriends) {
           TbUser tbUser = tbUserMapper.selectByPrimaryKey(tbFriend.getFriendsId());
           User user = new User();
           BeanUtils.copyProperties(tbUser,user);
           users.add(user);
       }

        return users;
   }

}
